package com.easy.customer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.CustomerContacts;
import com.easy.common.bean.Customers;
import com.easy.common.enumbean.Gender;
import com.easy.common.service.ICustomerService;
import com.easy.common.util.SYSUtil;
import com.easy.customer.mapper.CustomerContactsMapper;
import com.easy.customer.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class CustomerService implements ICustomerService {
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CustomerContactsMapper contactsMapper;
    //确保事务
    @Transactional
    @Override
    public Customers insert(Customers customers) {
        //保存客户信息
        customerMapper.insert(customers);
        //保存客户联系方式
        List<CustomerContacts> list=customers.getContacts();
        //检查非空
        if(SYSUtil.checkNotNull(list)&&!list.isEmpty()){
            for(CustomerContacts item:list){
                //设置客户的id
                item.setCustomer_id(customers.getId());
                //保存联系方式
                contactsMapper.insert(item) ;
            }
        }
        Customers result=customerMapper.selectById(customers.getId());
        return result;
    }

    @Override
    public Customers update(Customers customers) {
        customerMapper.updateById(customers);
        Customers result=customerMapper.selectById(customers.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int result=customerMapper.deleteById(id);
        return result;
    }

    @Override
    public Customers getByID(Integer id) {
        Customers customers=customerMapper.selectById(id);
        return customers;
    }

    @Override
    public IPage<Customers> getListByPage(IPage page, Customers customers) {
        QueryWrapper wrapper=new QueryWrapper(customers);
        IPage<Customers> result=customerMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public List<Customers> getList(Customers customers) {
        QueryWrapper wrapper=new QueryWrapper(customers);
        List<Customers> result=customerMapper.selectList(wrapper);
        return result;
    }
}
